package com.anthonybennis.blender.client.pages.bestwebsites;

import java.util.List;

import com.anthonybennis.blender.client.UIConstants;
import com.anthonybennis.blender.client.images.ImageClientBundle;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author abennis
 */
public class BestWebsitePage 
{
	private final ImageClientBundle _imageClientBunde;
	
	/**
	 * 
	 * @param imageClientBunde
	 */
	public BestWebsitePage(ImageClientBundle imageClientBunde)
	{
		_imageClientBunde = imageClientBunde;
	}
	
	/**
	 * 
	 * @return
	 */
	public Widget create()
	{
		DockLayoutPanel webPagesPanel = new DockLayoutPanel(Unit.EM);
		webPagesPanel.setSize("100%", "100%");
		Frame browser = this.createBrowser();
		Widget navBar = this.createNavigationPanel(browser);
		webPagesPanel.addWest(navBar, UIConstants.NAVIGATION_PANEL_WIDTH);
		
		
		webPagesPanel.add(browser);
		
		return webPagesPanel;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget createNavigationPanel(Frame browser)
	{
		final FlowPanel leftSideNavBar = new FlowPanel();
		
		leftSideNavBar.setStyleName("nav-panel");
		final BestWebsitesDataGenerator dataGenerator = new BestWebsitesDataGenerator(_imageClientBunde);
		final BestWebsiteWidgetFactory widgetFactory = new BestWebsiteWidgetFactory(browser);
		final List<WebpageTileData> webPageTileData = dataGenerator.createGridData();
		
		for (WebpageTileData tileData : webPageTileData) 
		{
			Widget tileWidget = widgetFactory.create(tileData);
			leftSideNavBar.add(tileWidget);
		}
		
		return leftSideNavBar;
	}
	
	/**
	 * 
	 * @return
	 */
	private Frame createBrowser()
	{
		Frame browser = new Frame(_imageClientBunde.webpageInstructions().getSafeUri().asString());
		browser.setSize("100%", "100%");
		
		return browser;
	}
}